﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TowerDefender.Entities;
using System.Windows.Controls;
using System.Windows.Media.Animation;
using System.Windows.Media;
using TowerDefender.Model;
using TowerDefender.Graphics.Animated.Mobs;

namespace TowerDefender.Graphics
{
    public class PeonManager
    {

        public static PeonGraphics CreatePeonGraphics(Peon peon, Map m, Grid g, double stepX, double stepY, Engine engine)
        {
            PeonGraphics pg = new PeonGraphics(peon, stepX, stepY, engine);
            g.Children.Add(pg);
            return pg;
        }

        public static void DrawPeon(PeonGraphics peonGraphics, Peon peon)
        {
            peonGraphics.SetValue(Grid.ColumnProperty, peon.coordinates.X - 1);
            peonGraphics.SetValue(Grid.RowProperty, peon.coordinates.Y - 1);
        }
        
        public static void MovePeon(PeonGraphics peonGraphics)
        {
            DoubleAnimation da = new DoubleAnimation();
            da.Completed += new EventHandler(peonGraphics.da_Completed);
            da.Duration = TimeSpan.FromSeconds(1);
            TranslateTransform tt = new TranslateTransform();
            peonGraphics.RenderTransform = tt;
            Direction dir = GetDirection(peonGraphics.OldPosition, peonGraphics.peon.PeonMap.MapPath.Steps[peonGraphics.peon.PositionInMapPath]);
            peonGraphics.peon.InMotion = true;

            switch (dir)
            {
                case Direction.Left:
                    da.By = (-1) * peonGraphics.StepX;
                    tt.BeginAnimation(TranslateTransform.XProperty, da);
                    break;
                case Direction.Right:
                    da.By = peonGraphics.StepX;
                    tt.BeginAnimation(TranslateTransform.XProperty, da);
                    break;
                case Direction.Up:
                    da.By = (-1) * peonGraphics.StepY;
                    tt.BeginAnimation(TranslateTransform.YProperty, da);
                    break;
                case Direction.Down:
                    da.By = peonGraphics.StepY;
                    tt.BeginAnimation(TranslateTransform.YProperty, da);
                    break;
                case Direction.None:
                    peonGraphics.peon.InMotion = false;
                    break;
                default:
                    peonGraphics.peon.InMotion = false;
                    break;
            }
        }

        public enum Direction { Left, Right, Up, Down, None };

        public static Direction GetDirection(Coordinates oldPos, Coordinates newPos)
        {
            if (newPos.X - oldPos.X > 0)
            {
                return Direction.Right;
            }
            else if (newPos.X - oldPos.X < 0)
            {
                return Direction.Left;
            }
            else if (newPos.Y - oldPos.Y > 0)
            {
                return Direction.Down;
            }
            else if (newPos.Y - oldPos.Y < 0)
            {
                return Direction.Up;
            }

            return Direction.None;
        }
    }
}
